//
//  HJJRadioViewController.m
//  Pianke
//
//  Created by 黄俊杰 on 15/12/22.
//  Copyright © 2015年 dada. All rights reserved.
//

#import "HJJRadioViewController.h"
#import "HJJRootHeadView.h"
#import "HJJRadioRootCell.h"
#import "HJJRadioRootModel.h"
#import "HJJCarousel.h"
#import "HJJRadioDetailViewController.h"

@interface HJJRadioViewController ()<UITableViewDataSource, UITableViewDelegate, RadioRootHeadViewDelegate>

@property (nonatomic, strong)HJJRootHeadView *rootHeadView;

@property (nonatomic, strong)NSMutableArray *picArray;

@property (nonatomic, strong)NSMutableArray *radioArray;

@property (nonatomic, strong)NSMutableArray *underPicArray;

@property (nonatomic, strong)UITableView *myTableView;

@property(nonatomic,assign)NSInteger nextPage;//记录下一页

@property(nonatomic,assign)BOOL isUpLoading;//标记上拉操作还是下拉操作，yes为上拉

@end

@implementation HJJRadioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.underPicArray = [NSMutableArray array];
    self.picArray = [NSMutableArray array];
    self.radioArray = [NSMutableArray array];
    
    //头视图初始化
    self.rootHeadView = [[HJJRootHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250 * EACH_HEIGHT)];
    self.rootHeadView.headViewDele = self;
    [self automaticallyAdjustsScrollViewInsets];
    
    //tableView
    self.myTableView = [[UITableView alloc] init];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    self.myTableView.tableHeaderView = self.rootHeadView;
    [self.myTableView registerClass:[HJJRadioRootCell class] forCellReuseIdentifier:@"HJJRadioRootCell"];
    
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(0);
        make.left.bottom.right.mas_equalTo(0);
    }];
    
    //上下拉刷新
    [self addHeader];
    [self addFooter];
}

-(void)addHeader{
    
    MJRefreshGifHeader *header = [WWY_AppTools headerWithRefreshingBlock:^{
        self.nextPage = 0;
        self.isUpLoading = NO;
        [self getData];
        
    }];
    self.myTableView.mj_header = header;
    [self.myTableView.mj_header beginRefreshing];
    
    
}
-(void)addFooter{
    
    MJRefreshBackGifFooter *footer = [WWY_AppTools footerWithRefreshingBlock:^{
        
        self.nextPage += 9;
        self.isUpLoading = YES;
        [self getCellData];
        
    }];
    self.myTableView.mj_footer = footer;
    
    
}

-(void)getData{

    NSString *url = @"http://api2.pianke.me/ting/radio";
    NSString *body = @"auth=&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&version=3.0.6";
    NSDictionary *headFile = @{@"PHPSESSID":@"58ts41v1nohdn89damknm0vdt5"};
    [LDJ_AppTools postUrl:url body:body result:LDJJSON requestStyle:LDJRequestString headerFile:headFile success:^(id result) {
        
        NSMutableArray *carousel = [[result objectForKey:@"data"] objectForKey:@"carousel"];
        for (NSMutableDictionary *carouselDic in carousel) {
            HJJCarousel *carousel = [[HJJCarousel alloc] init];
            [carousel setValuesForKeysWithDictionary:carouselDic];
            [self.picArray addObject:carousel];
        }
        
        [self.rootHeadView sendPicArray:self.picArray];
        
        NSMutableArray *hotlist = [[result objectForKey:@"data"] objectForKey:@"hotlist"];
        for (NSMutableDictionary *hotlistDic in hotlist) {
            HJJRadioRootModel *radio = [[HJJRadioRootModel alloc]init];
            [radio setValuesForKeysWithDictionary:hotlistDic];
            [self.underPicArray addObject:radio];
        }
        
        [self.rootHeadView sendUnderArray:self.underPicArray];
        
        NSMutableArray *alllist = [[result objectForKey:@"data"] objectForKey:@"alllist"];
        for (NSMutableDictionary *alllistDic in alllist) {
            HJJRadioRootModel *radio = [[HJJRadioRootModel alloc] init];
            [radio setValuesForKeysWithDictionary:alllistDic];
            radio.uname = [[alllistDic objectForKey:@"userinfo"] objectForKey:@"uname"];
            [self.radioArray addObject:radio];
        }
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
        [self.myTableView reloadData];
        
    } failure:^(NSError *error) {
        
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
    }];
}

-(void)getCellData{
    
    
    NSString *url = @"http://api2.pianke.me/ting/radio_list";
    NSString *body = [NSString stringWithFormat:@"auth=&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&limit=9&start=%ld&version=3.0.6", self.nextPage];
    [LDJ_AppTools postUrl:url body:body result:LDJJSON requestStyle:LDJRequestString headerFile:nil success:^(id result) {
        
        NSMutableArray *alllist = [[result objectForKey:@"data"] objectForKey:@"list"];
//        NSLog(@"-----%@",result);
        for (NSMutableDictionary *alllistDic in alllist) {
            HJJRadioRootModel *radio = [[HJJRadioRootModel alloc] init];
            [radio setValuesForKeysWithDictionary:alllistDic];
            radio.uname = [[alllistDic objectForKey:@"userinfo"] objectForKey:@"uname"];
            [self.radioArray addObject:radio];
//            NSLog(@"hhhhhhh");
        }
        [self.myTableView.mj_footer endRefreshing];
        [self.myTableView reloadData];
        NSLog(@"%ld,%ld",self.nextPage,self.radioArray.count);
        
    } failure:^(NSError *error) {
        
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
    }];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 110;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.radioArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellId = @"HJJRadioRootCell";
    HJJRadioRootCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    HJJRadioRootModel *model = [self.radioArray objectAtIndex:indexPath.row];
    [cell sendModel:model];
    return cell;
    // Dispose of any resources that can be recreated.
}

//点击cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:1];
    HJJRadioDetailViewController *radioDetailVC = [[HJJRadioDetailViewController alloc] init];
    HJJRadioRootModel *model = [self.radioArray objectAtIndex:indexPath.row];
    radioDetailVC.radioid = model.radioid;
    radioDetailVC.naviTitle = model.title;
    [self.navigationController pushViewController:radioDetailVC animated:1];
    
}

//协议方法
-(void)goToNextView:(NSString *)key{

    HJJRadioDetailViewController *radioDetailVC = [[HJJRadioDetailViewController alloc] init];
    HJJRadioRootModel *model = [self.underPicArray objectAtIndex:[key integerValue]];
    radioDetailVC.radioid = model.radioid;
    radioDetailVC.naviTitle = model.title;
    [self.navigationController pushViewController:radioDetailVC animated:1];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
